﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Xml.Linq;
using HPPClientLibrary;
using HPPClientLibrary.DownLoad;
using HPPNet;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections;
using HPPUtil;
using Moq;
using BitArray=HPPUtil.BitArray;

namespace HPP.UnitTest
{
    /// <summary>
    ///这是 CSServerTest 的测试类，旨在
    ///包含所有 CSServerTest 单元测试
    ///</summary>
    [TestClass()]
    public class CSServerTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get { return testContextInstance; }
            set { testContextInstance = value; }
        }

        private PrivateObject param0 = null;
        private CSServer_Accessor target;

        #region 附加测试属性

        // 
        //编写测试时，还可使用以下属性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //使用 TestInitialize 在运行每个测试前先运行代码
        [TestInitialize()]
        public void MyTestInitialize()
        {
            param0 = new PrivateObject(new CSServer(new HPPClient(), 0x3, 0xFF));
            target = new CSServer_Accessor(param0);
        }

        //使用 TestCleanup 在运行完每个测试后运行代码
        [TestCleanup()]
        public void MyTestCleanup()
        {
            param0 = null;
            target = null;
            
        }
        

        #endregion

        #region ParseUrl

        /// <summary>
        ///ParseUrl 的测试
        /// 
        /// 输入：url is null
        /// 
        /// 预期输出: out Hashtable is null
        ///</summary>
        /// <remarks>
        /// 2009-08-20
        /// vls
        /// </remarks>
        [TestMethod()]
        [DeploymentItem("HPPClientLibrary.dll")]
        public void Test_ParseUrlTest_when_url_is_null()
        {
            Hashtable urlInfo;

            target.ParseUrl(out urlInfo, null);

            Assert.AreEqual(null, urlInfo);
        }

        /// <summary>
        ///ParseUrl 的测试
        /// 
        /// 输入：url is empty
        /// 
        /// 预期输出: out Hashtable is null
        ///</summary>
        /// <remarks>
        /// 2009-08-20
        /// vls
        /// </remarks>
        [TestMethod()]
        [DeploymentItem("HPPClientLibrary.dll")]
        public void Test_ParseUrlTest_when_url_is_empty()
        {
            Hashtable urlInfo;

            target.ParseUrl(out urlInfo, string.Empty);

            Assert.AreEqual(null, urlInfo);
        }

        /// <summary>
        ///ParseUrl 的测试
        /// 
        /// 输入：url has no prefix slash
        /// 
        /// 预期输出: URL被正确解析
        ///</summary>
        /// <remarks>
        /// 2009-08-20
        /// vls
        /// </remarks>
        [TestMethod()]
        [DeploymentItem("HPPClientLibrary.dll")]
        public void Test_ParseUrlTest_when_url_has_no_prefix_slash()
        {
            Hashtable urlInfo;

            target.ParseUrl(out urlInfo, "abc.txt|hash/");


            Assert.AreEqual("abc.txt", urlInfo["FileName"]);
            Assert.AreEqual("hash", urlInfo["Hash"]);
        }

        /// <summary>
        ///ParseUrl 的测试
        /// 
        /// 输入：url has no suffix slash
        /// 
        /// 预期输出: URL被正确解析
        ///</summary>
        /// <remarks>
        /// 2009-08-20
        /// vls
        /// </remarks>
        [TestMethod()]
        [DeploymentItem("HPPClientLibrary.dll")]
        public void Test_ParseUrlTest_when_url_has_no_suffix_slash()
        {
            Hashtable urlInfo;

            target.ParseUrl(out urlInfo, "/abc.txt|hash");


            Assert.AreEqual("abc.txt", urlInfo["FileName"]);
            Assert.AreEqual("hash", urlInfo["Hash"]);
        }

        /// <summary>
        ///ParseUrl 的测试
        /// 
        /// 输入：url has no prefix and suffix slash
        /// 
        /// 预期输出: URL被正确解析
        ///</summary>
        /// <remarks>
        /// 2009-08-20
        /// vls
        /// </remarks>
        [TestMethod()]
        [DeploymentItem("HPPClientLibrary.dll")]
        public void Test_ParseUrlTest_when_url_has_no_prefix_and_suffix_slash()
        {
            Hashtable urlInfo;

            target.ParseUrl(out urlInfo, "abc.txt|hash");


            Assert.AreEqual("abc.txt", urlInfo["FileName"]);
            Assert.AreEqual("hash", urlInfo["Hash"]);
        }

        /// <summary>
        ///ParseUrl 的测试
        /// 
        /// 输入：url has no pipe
        /// 
        /// 预期输出: Hashtable is null
        ///</summary>
        /// <remarks>
        /// 2009-08-20
        /// vls
        /// </remarks>
        [TestMethod()]
        [DeploymentItem("HPPClientLibrary.dll")]
        public void Test_ParseUrlTest_when_url_has_no_pipe()
        {
            Hashtable urlInfo;

            target.ParseUrl(out urlInfo, "/abc.txthash/");

            Assert.AreEqual(null, urlInfo);
        }

        /// <summary>
        ///ParseUrl 的测试
        /// 
        /// 输入：url has prefix and suffix slash
        /// 
        /// 预期输出: URL被正确解析
        ///</summary>
        /// <remarks>
        /// 2009-08-20
        /// vls
        /// </remarks>
        [TestMethod()]
        [DeploymentItem("HPPClientLibrary.dll")]
        public void Test_ParseUrlTest_when_url_has_prefix_and_suffix_slash()
        {
            Hashtable urlInfo;

            target.ParseUrl(out urlInfo, "/abc.txt|hash/");


            Assert.AreEqual("abc.txt", urlInfo["FileName"]);
            Assert.AreEqual("hash", urlInfo["Hash"]);
        }


        /// <summary>
        ///ParseUrl 的测试
        /// 
        /// 输入： url has blocknum
        /// 
        /// 预期输出: hashtable is null
        ///</summary>
        /// <remarks>
        /// 2009-08-20
        /// vls
        /// </remarks>
        [TestMethod()]
        [DeploymentItem("HPPClientLibrary.dll")]
        public void Test_ParseUrlTest_when_url_has_blocknum()
        {
            Hashtable urlInfo;

            target.ParseUrl(out urlInfo, "/abc.txt|hash/23");

            Assert.AreEqual("abc.txt", urlInfo["FileName"]);
            Assert.AreEqual("hash", urlInfo["Hash"]);
            Assert.AreEqual("23", urlInfo["BlockNum"]);
        }

        #endregion ParseUrl

        #region GetOwnedFileBlock

        /// <summary>
        /// GetOwnedFileBlock 的测试
        /// 
        /// 输入： filehash is null
        /// 
        /// 预期输出: ClientA-向ClientB查询ClientB拥有某个资源R的分块信息，ClientB没有R时，ClientB向ClientA返回数据的格式
        ///</summary>
        /// <remarks>
        /// 2009-08-20
        /// vls
        /// </remarks>
        [TestMethod()]
        [DeploymentItem("HPPClientLibrary.dll")]
        public void Test_GetOwnedFileBlock_when_filehash_is_null()
        {
            SetupIClient();

            string xmlString = target.GetOwnedFileBlocks(null);

            XDocument doc = XmlAssert.AssertValid(xmlString);

            Assert.AreEqual(false, doc.Descendants("File").Single().HasElements);
        }


        /// <summary>
        /// GetOwnedFileBlock 的测试
        /// 
        /// 输入： filehash is empty
        /// 
        /// 预期输出: ClientA-向ClientB查询ClientB拥有某个资源R的分块信息，ClientB没有R时，ClientB向ClientA返回数据的格式
        ///</summary>
        /// <remarks>
        /// 2009-08-23
        /// vls
        /// </remarks>
        [TestMethod()]
        [DeploymentItem("HPPClientLibrary.dll")]
        public void Test_GetOwnedFileBlock_when_filehash_is_empty()
        {
            SetupIClient();

            string xmlString = target.GetOwnedFileBlocks(string.Empty);

            XDocument doc = XmlAssert.AssertValid(xmlString);

            Assert.AreEqual(false, doc.Descendants("File").Single().HasElements);
        }

        /// <summary>
        /// GetOwnedFileBlock 的测试
        /// 
        /// 输入： 不存在的filehash
        /// 
        /// 预期输出: ClientA-向ClientB查询ClientB拥有某个资源R的分块信息，ClientB没有R时，ClientB向ClientA返回数据的格式
        ///</summary>
        /// <remarks>
        /// 2009-08-23
        /// vls
        /// </remarks>
        [TestMethod()]
        [DeploymentItem("HPPClientLibrary.dll")]
        public void Test_GetOwnedFileBlock_when_filehash_does_not_exist()
        {
            SetupIClient();

            string xmlString = target.GetOwnedFileBlocks("FFFFFFx.s.2");

            XDocument doc = XmlAssert.AssertValid(xmlString);

            Assert.AreEqual(false, doc.Descendants("File").Single().HasElements);
        }

        /// <summary>
        /// GetOwnedFileBlock 的测试
        /// 
        /// 输入： 存在的filehash
        /// 
        /// 预期输出: ClientA-向ClientB查询ClientB拥有某个资源R的分块信息，ClientB拥有R时，ClientB向ClientA返回数据的格式
        ///</summary>
        /// <remarks>
        /// 2009-08-23
        /// vls
        /// </remarks>
        [TestMethod()]
        [DeploymentItem("HPPClientLibrary.dll")]
        public void Test_GetOwnedFileBlock_when_filehash_exists()
        {
            SetupIClient();

            string xmlString = target.GetOwnedFileBlocks("testhash");

            XDocument doc = XmlAssert.AssertValid(xmlString);

            Assert.AreEqual(true, doc.Descendants("File").Single().HasElements);

            Assert.AreEqual("testhash", doc.Descendants("FileHash").Single().Attribute("Hash").Value);

            Assert.AreEqual("20008082", doc.Descendants("HasBlocks").Single().Attribute("Blocks").Value);
        }

        /// <summary>
        /// GetOwnedFileBlock 的测试
        /// 
        /// 输入: 自己共享的文件hash
        /// 
        /// 预期输出: ClientA-向ClientB查询ClientB拥有某个资源R的分块信息，ClientB拥有R时，ClientB向ClientA返回数据的格式
        ///</summary>
        /// <remarks>
        /// 2009-09-27
        /// vls
        /// </remarks>
        [TestMethod()]
        [DeploymentItem("HPPClientLibrary.dll")]
        public void Test_GetOwnedFileBlock_when_filehash_shared_by_me()
        {
            var mockIClient = new Mock<IClient>();
            mockIClient.Setup(c => c.ReadOnlyKnownDict).Returns(new HashSet<string>() {"testhash"});


            long len = BasicSetting.BlockSize*10;

            mockIClient.Setup(c => c.ReadOnlyHashFileInfoDict).Returns(
                new ReadOnlyDictionary<string, HppFileInfo>(new Dictionary<string, HppFileInfo>()
                                                                {
                                                                    {
                                                                        "testhash", new HppFileInfo()
                                                                                        {
                                                                                            FullPathName = "c:\\123.txt",
                                                                                            Len = len
                                                                                        }
                                                                        }
                                                                }));

            string xmlString = target.GetOwnedFileBlocks("testhash");

            XDocument doc = XmlAssert.AssertValid(xmlString);

            Assert.AreEqual(true, doc.Descendants("File").Single().HasElements);

            Assert.AreEqual("testhash", doc.Descendants("FileHash").Single().Attribute("Hash").Value);

            Assert.AreEqual("3FF", doc.Descendants("HasBlocks").Single().Attribute("Blocks").Value);
        }

        #endregion

        #region GetDownloadFile

        /// <summary>
        /// GetDownloadFile 的测试
        /// 
        /// 输入： filehash is null
        /// 
        /// 预期输出: FileResponse is null
        ///</summary>
        /// <remarks>
        /// 2009-08-23
        /// vls
        /// </remarks>
        [TestMethod()]
        [DeploymentItem("HPPClientLibrary.dll")]
        public void Test_GetDownloadFile_when_filehash_is_null()
        {
            SetupIClient();

            FileResponse actual = target.GetDownLoadFile(null, 1);

            Assert.AreEqual(null, actual);
        }

        /// <summary>
        /// GetDownloadFile 的测试
        /// 
        /// 输入： filehash is empty
        /// 
        /// 预期输出: FileResponse is null
        ///</summary>
        /// <remarks>
        /// 2009-08-23
        /// vls
        /// </remarks>
        [TestMethod()]
        [DeploymentItem("HPPClientLibrary.dll")]
        public void Test_GetDownloadFile_when_filehash_is_empty()
        {
            SetupIClient();

            FileResponse actual = target.GetDownLoadFile(string.Empty, 1);

            Assert.AreEqual(null, actual);
        }


        /// <summary>
        /// GetDownloadFile 的测试
        /// 
        /// 输入： filehash exists and blockNum doesn't exist
        /// 
        /// 预期输出: FileResponse is null
        ///</summary>
        /// <remarks>
        /// 2009-08-23
        /// vls
        /// </remarks>
        [TestMethod()]
        [DeploymentItem("HPPClientLibrary.dll")]
        public void Test_GetDownloadFile_when_filehash_exists_and_blockNum_does_not_exist()
        {
            SetupIClient();

            FileResponse actual = target.GetDownLoadFile("testhash", 1);

            Assert.AreEqual(null, actual);
        }

        /// <summary>
        /// GetDownloadFile 的测试
        /// 
        /// 输入： filehash exists and blockNum_exists
        /// 
        /// 预期输出: 对应信息正确的FileResponse
        ///</summary>
        /// <remarks>
        /// 2009-08-26
        /// vls
        /// </remarks>
        [TestMethod()]
        [DeploymentItem("HPPClientLibrary.dll")]
        public void Test_GetDownloadFile_when_filehash_exists_and_blockNum_exists()
        {
            SetupIClient();
            int blockNum = 8;
            FileResponse actual = target.GetDownLoadFile("testhash", blockNum);


            Assert.IsInstanceOfType(actual, typeof (FileResponse));
            Assert.AreEqual("c:\\123.txt", actual.FileName);
            Assert.AreEqual(true, actual.HasRange);
            Assert.AreEqual((blockNum - 1)*target.BLOCKSIZE, actual.RangeBegin);
            Assert.AreEqual(blockNum*target.BLOCKSIZE - 1, actual.RangeEnd);
            Assert.AreEqual(true, actual.RangeValid);
            Assert.AreEqual(target.BLOCKSIZE, actual.Length);
        }

        /// <summary>
        /// GetDownloadFile 的测试
        /// 
        /// 输入： filehash exists and blockNum is the last
        /// 
        /// 预期输出: 对应信息正确的FileResponse
        ///</summary>
        /// <remarks>
        /// 2009-08-26
        /// vls
        /// </remarks>
        [TestMethod()]
        [DeploymentItem("HPPClientLibrary.dll")]
        public void Test_GetDownloadFile_when_filehash_exists_and_blockNum_is_the_last()
        {
            SetupIClient(31, 0, new List<int> {31});
            int blockNum = 31;
            FileResponse actual = target.GetDownLoadFile("testhash", blockNum);


            Assert.IsInstanceOfType(actual, typeof (FileResponse));
            Assert.AreEqual("c:\\123.txt", actual.FileName);
            Assert.AreEqual(true, actual.HasRange);
            Assert.AreEqual((blockNum - 1)*target.BLOCKSIZE, actual.RangeBegin);
            Assert.AreEqual(blockNum*target.BLOCKSIZE - 1, actual.RangeEnd);
            Assert.AreEqual(true, actual.RangeValid);
            Assert.AreEqual(target.BLOCKSIZE, actual.Length);
        }


        /// <summary>
        /// GetDownloadFile 的测试
        /// 
        /// 输入： filehash exists and blockNum is the last
        /// 
        /// 预期输出: 对应信息正确的FileResponse
        ///</summary>
        /// <remarks>
        /// 2009-08-26
        /// vls
        /// </remarks>
        [TestMethod()]
        [DeploymentItem("HPPClientLibrary.dll")]
        public void Test_GetDownloadFile_when_filehash_exists_and_blockNum_is_the_last_2nd()
        {
            SetupIClient(31, 100, new List<int> {31, 32});
            int blockNum = 32;
            FileResponse actual = target.GetDownLoadFile("testhash", blockNum);
            Assert.IsInstanceOfType(actual, typeof (FileResponse));
            Assert.AreEqual("c:\\123.txt", actual.FileName);
            Assert.AreEqual(true, actual.HasRange);
            Assert.AreEqual((blockNum - 1)*target.BLOCKSIZE, actual.RangeBegin);
            Assert.AreEqual((blockNum - 1)*target.BLOCKSIZE + 100 - 1, actual.RangeEnd);
            Assert.AreEqual(true, actual.RangeValid);
            Assert.AreEqual(100, actual.Length);
        }

        /// <summary>
        /// GetDownloadFile 的测试
        /// 
        /// 输入： filehash exists and blockNum is the last
        /// 
        /// 预期输出: 对应信息正确的FileResponse
        ///</summary>
        /// <remarks>
        /// 2009-08-26
        /// vls
        /// </remarks>
        [TestMethod()]
        [DeploymentItem("HPPClientLibrary.dll")]
        public void
            Test_GetDownloadFile_when_filehash_exists_and_blockNum_is_the_last_and_lastBlockSize_not_equal_to_blockSize()
        {
            SetupIClient(31, 100, new List<int> {31, 32});
            int blockNum = 32;
            FileResponse actual = target.GetDownLoadFile("testhash", blockNum);
            Assert.IsInstanceOfType(actual, typeof (FileResponse));
            Assert.AreEqual("c:\\123.txt", actual.FileName);
            Assert.AreEqual(true, actual.HasRange);
            Assert.AreEqual((blockNum - 1)*target.BLOCKSIZE, actual.RangeBegin);
            Assert.AreEqual((blockNum - 1)*target.BLOCKSIZE + 100 - 1, actual.RangeEnd);
            Assert.AreEqual(true, actual.RangeValid);
            Assert.AreEqual(100, actual.Length);
        }

        /// <summary>
        /// GetDownloadFile 的测试
        /// 
        /// 输入： filehash does not exists in downloadManager
        /// 
        /// 预期输出: null
        ///</summary>
        /// <remarks>
        /// 2009-09-12
        /// vls
        /// </remarks>
        [TestMethod()]
        [DeploymentItem("HPPClientLibrary.dll")]
        public void Test_GetDownloadFile_when_filehash_does_not_exist_in_downloadManager()
        {
            var mockIClient = new Mock<IClient>();
            mockIClient.Setup(c => c.DownloadManager.HashJobInfoDict).Returns(new Dictionary<string, JobInfo>()
                                                                                  {
                                                                                  });

            mockIClient.SetupGet(c => c.ReadOnlyHashFileInfoDict).Returns(
                new ReadOnlyDictionary<string, HppFileInfo>(new Dictionary<string, HppFileInfo>()
                                                                {
                                                                    {
                                                                        "testhash",
                                                                        new HppFileInfo()
                                                                            {FullPathName = "c:\\123.txt", Len = 123}
                                                                        }
                                                                }));


            target = new CSServer_Accessor(new PrivateObject(new CSServer(mockIClient.Object, 0xFF, 0xFF)));
            int blockNum = 32;
            FileResponse actual = target.GetDownLoadFile("testhash", blockNum);
            Assert.IsNull(actual);
            
        }

        /// <summary>
        /// GetDownloadFile 的测试
        /// 
        /// 输入： filehash does not exists in HashFileInfoDict
        /// 
        /// 预期输出: null
        ///</summary>
        /// <remarks>
        /// 2009-09-12
        /// vls
        /// </remarks>
        [TestMethod()]
        [DeploymentItem("HPPClientLibrary.dll")]
        public void Test_GetDownloadFile_when_filehash_does_not_exist_in_HashFullNameDict()
        {
            var mockIClient = new Mock<IClient>();
            int blockNum = 32;
            int len = target.BLOCKSIZE*blockNum;

            int realBlockCount = (len + target.BLOCKSIZE - 1)/target.BLOCKSIZE;

            BitArray array = new BitArray(realBlockCount);

            array.Set(32, true);

            JobInfo info = new JobInfo(null, "abc.txt")
                               {
                                   LocalFileBlocks = array
                               };
            mockIClient.Setup(c => c.DownloadManager.HashJobInfoDict).Returns(new Dictionary<string, JobInfo>()
                                                                                  {
                                                                                      {"testhash", info}
                                                                                  });

            mockIClient.SetupGet(c => c.ReadOnlyHashFileInfoDict).Returns(
                new ReadOnlyDictionary<string, HppFileInfo>(new Dictionary<string, HppFileInfo>()
                    ));


            target = new CSServer_Accessor(new PrivateObject(new CSServer(mockIClient.Object, 0xFF, 0xFF)));

            FileResponse actual = target.GetDownLoadFile("testhash", blockNum);
            Assert.IsNull(actual);
        }

        /// <summary>
        /// GetDownloadFile 的测试
        /// 
        /// 输入： 自己共享的文件的Hash
        /// 
        /// 预期输出: 对应信息正确的FileResponse
        ///</summary>
        /// <remarks>
        /// 2009-09-12
        /// vls
        /// </remarks>
        [TestMethod()]
        [DeploymentItem("HPPClientLibrary.dll")]
        public void Test_GetDownloadFile_when_filehash_shared_by_me()
        {
            var mockIClient = new Mock<IClient>();

            mockIClient.Setup(c => c.ReadOnlyKnownDict).Returns(new HashSet<string>() {"testhash"});

            long fileLen = BasicSetting.BlockSize*10;
            mockIClient.Setup(c => c.ReadOnlyHashFileInfoDict).Returns(
                new ReadOnlyDictionary<string, HppFileInfo>(new Dictionary<string, HppFileInfo>()
                                                                {
                                                                    {
                                                                        "testhash", new HppFileInfo()
                                                                                        {
                                                                                            FullPathName = "c:\\123.txt",
                                                                                            Len = fileLen
                                                                                        }
                                                                    }
                                                                }));

            int blockNum = 8;
            FileResponse actual = target.GetDownLoadFile("testhash", blockNum);


            Assert.IsInstanceOfType(actual, typeof(FileResponse));
            Assert.AreEqual("c:\\123.txt", actual.FileName);
            Assert.AreEqual(true, actual.HasRange);
            Assert.AreEqual((blockNum - 1) * target.BLOCKSIZE, actual.RangeBegin);
            Assert.AreEqual(blockNum * target.BLOCKSIZE - 1, actual.RangeEnd);
            Assert.AreEqual(true, actual.RangeValid);
            Assert.AreEqual(target.BLOCKSIZE, actual.Length);
        }

        #endregion

        #region ProcessHttpReq

        /// <summary>
        /// ProcessHttpReq 的测试
        /// 
        /// 输入： remoteEndPoint is null
        /// 
        /// 预期输出: response is null
        ///</summary>
        /// <remarks>
        /// 2009-08-27
        /// vls
        /// </remarks>
        [TestMethod()]
        [DeploymentItem("HPPClientLibrary.dll")]
        public void Test_ProcessHttpReq_when_remoteEndPoint_is_null()
        {
            Response actual = target.ProcessHttpReq(null, new Hashtable() {{"Url", "/123.txt|hash"}}, "body");
            Assert.AreEqual(null, actual);
        }

        /// <summary>
        /// ProcessHttpReq 的测试
        /// 
        /// 输入： headers doesn't contain Url
        /// 
        /// 预期输出: response is null
        ///</summary>
        /// <remarks>
        /// 2009-08-27
        /// vls
        /// </remarks>
        [TestMethod()]
        [DeploymentItem("HPPClientLibrary.dll")]
        public void Test_ProcessHttpReq_when_headers_does_not_contain_url()
        {
            Response actual = target.ProcessHttpReq(new IPEndPoint(IPAddress.Loopback, 0xFF), new Hashtable(), "body");
            Assert.AreEqual(null, actual);
        }

        /// <summary>
        /// ProcessHttpReq 的测试
        /// 
        /// 输入： 有blockNum的查询
        /// 
        /// 预期输出: response is FileResponse
        ///</summary>
        /// <remarks>
        /// 2009-08-27
        /// vls
        /// </remarks>
        [TestMethod()]
        [DeploymentItem("HPPClientLibrary.dll")]
        public void Test_ProcessHttpReq_when_there_is_blockNum()
        {
            SetupIClient();

            Response actual = target.ProcessHttpReq(new IPEndPoint(IPAddress.Loopback, 0xFF),
                                                    new Hashtable() {{"Url", "/123.txt|testhash/2"}}, "body");
            Assert.IsInstanceOfType(actual, typeof (FileResponse));
        }

        /// <summary>
        /// ProcessHttpReq 的测试
        /// 
        /// 输入： 无blockNum的查询
        /// 
        /// 预期输出: response is StringResponse
        ///</summary>
        /// <remarks>
        /// 2009-08-27
        /// vls
        /// </remarks>
        [TestMethod()]
        [DeploymentItem("HPPClientLibrary.dll")]
        public void Test_ProcessHttpReq_when_there_is_no_blockNum()
        {
            SetupIClient();

            Response actual = target.ProcessHttpReq(new IPEndPoint(IPAddress.Loopback, 0xFF),
                                                    new Hashtable() {{"Url", "/123.txt|testhash"}}, "body");
            Assert.IsInstanceOfType(actual, typeof (StringResponse));
        }

        #endregion

        /// <summary>
        /// 配置IClient的Mock
        /// </summary>
        /// <remarks>
        /// vls
        /// </remarks>
        private void SetupIClient()
        {
            SetupIClient(31, 100, new List<int>() {2, 8, 16, 30});
        }

        /// <summary>
        /// 配置IClient的Mock
        /// </summary>
        /// <param name="blockCount">预计block的数目</param>
        /// <param name="fileLenOffset">block数目*blockSize后添加的文件长度</param>
        /// <param name="ownedBlockList">自己拥有block的列表</param>
        private void SetupIClient(int blockCount, int fileLenOffset, IEnumerable<int> ownedBlockList)
        {
            var mockIClient = new Mock<IClient>();

            int len = target.BLOCKSIZE*blockCount + fileLenOffset;

            int realBlockCount = (len + target.BLOCKSIZE - 1)/target.BLOCKSIZE;

            BitArray array = new BitArray(realBlockCount);

            foreach (int i in ownedBlockList)
            {
                array.Set(i, true);
            }

            JobInfo info = new JobInfo(null, "abc.txt")
                               {
                                   LocalFileBlocks = array,
                                   FileLen = len
                               };
            mockIClient.Setup(c => c.DownloadManager.HashJobInfoDict).Returns(new Dictionary<string, JobInfo>()
                                                                                  {
                                                                                      {"testhash", info}
                                                                                  });

            mockIClient.SetupGet(c => c.ReadOnlyHashFileInfoDict).Returns(
                new ReadOnlyDictionary<string, HppFileInfo>(new Dictionary<string, HppFileInfo>()
                                                                {
                                                                    {
                                                                        "testhash",
                                                                        new HppFileInfo()
                                                                            {FullPathName = "c:\\123.txt", Len = 123}
                                                                        }
                                                                }));


            target = new CSServer_Accessor(new PrivateObject(new CSServer(mockIClient.Object, 0xFF, 0xFF)));
        }
    }
}